home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / graphics_libraries / text / availfonts.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  15KB  |  356 lines

  1. ;/* AvailFonts.c - Execute me to compile me with Lattice 5.10a
  2. LC -cfistq -v -y -j73 AvailFonts.c
  3. Blink FROM LIB:c.o,AvailFonts.o TO AvailFonts LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5.  
  6.  
  7. Copyright (c) 1992 Commodore-Amiga, Inc.
  8.  
  9. This example is provided in electronic form by Commodore-Amiga, Inc. for
  10. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  11. published by Addison-Wesley (ISBN 0-201-56774-1).
  12.  
  13. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  14. information on the correct usage of the techniques and operating system
  15. functions presented in these examples.  The source and executable code
  16. of these examples may only be distributed in free electronic form, via
  17. bulletin board or as part of a fully non-commercial and freely
  18. redistributable diskette.  Both the source and executable code (including
  19. comments) must be included, without modification, in any copy.  This
  20. example may not be published in printed form or distributed with any
  21. commercial product.  However, the programming techniques and support
  22. routines set forth in these examples may be used in the development
  23. of original executable software products for Commodore Amiga computers.
  24.  
  25. All other rights reserved.
  26.  
  27. This example is provided "as-is" and is subject to change; no
  28. warranties are made.  All use is at your own risk. No liability or
  29. responsibility is assumed.
  30. */
  31.  
  32. #include <exec/types.h>
  33. #include <dos/rdargs.h>
  34. #include <dos/dosextens.h>
  35. #include <intuition/intuition.h>
  36. #include <intuition/screens.h>
  37. #include <graphics/text.h>
  38. #include <graphics/displayinfo.h>
  39. #include <graphics/regions.h>
  40. #include <graphics/gfx.h>
  41. #include <libraries/diskfont.h>
  42. #include <utility/tagitem.h>
  43. #include <clib/exec_protos.h>
  44. #include <clib/dos_protos.h>
  45. #include <clib/layers_protos.h>
  46. #include <clib/alib_stdio_protos.h>
  47. #include <clib/intuition_protos.h>
  48. #include <clib/graphics_protos.h>
  49. #include <clib/diskfont_protos.h>
  50. #include <clib/utility_protos.h>
  51.  
  52. #ifdef LATTICE
  53. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  54. int chkabort(void) { return(0); }
  55. #endif
  56.  
  57. UBYTE *vers = "\0$VER: AvailFonts 36.3";
  58.  
  59. void MainLoop(void);
  60. ULONG StrLen(UBYTE *);
  61.  
  62. struct stringstruct {
  63.         UBYTE *string;
  64.         LONG charcount;
  65.         WORD stringwidth;
  66. };
  67.  
  68. UBYTE *alphabetstring = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  69. struct stringstruct fname, fheight, XDPI, YDPI, entrynum;
  70. struct Library *DiskfontBase, *IntuitionBase, *LayersBase, *GfxBase, *UtilityBase;
  71. struct Window *mywin;
  72. struct RastPort *mycliprp, myrp;
  73. struct Rectangle myrect;
  74. struct Region *new_region, *old_region;
  75. struct DrawInfo *mydrawinfo;
  76. struct AvailFontsHeader *afh;
  77. LONG fontheight, alphabetcharcount;
  78. WORD stringwidth;
  79.  
  80. void main(int argc, char **argv)
  81. {
  82.   struct TextFont *defaultfont = NULL;
  83.   struct TextAttr defaultfontattr = { "topaz.font", 9, 0, 0 };
  84.  
  85.   LONG afsize, afshortage, cliprectside;
  86.  
  87.   fname.string = "Font Name:  ";
  88.   fheight.string = "Font Height:  ";
  89.   XDPI.string = "X DPI:  ";
  90.   YDPI.string = "Y DPI:  ";
  91.   entrynum.string = "Entry #:  ";
  92.  
  93.   if (DiskfontBase = OpenLibrary("diskfont.library", 37L))              /* Open the libraries. */
  94.   {
  95.     if (IntuitionBase = OpenLibrary("intuition.library", 37L))
  96.     {
  97.       if (GfxBase = OpenLibrary("graphics.library", 37L))
  98.       {
  99.         if (LayersBase = OpenLibrary("layers.library", 37L))
  100.         {
  101.           if (UtilityBase = OpenLibrary("utility.library", 37L))
  102.           {
  103.             if (mywin = OpenWindowTags(NULL,                              /* Open that window. */
  104.                                     WA_SmartRefresh,TRUE,
  105.                                     WA_SizeGadget,  FALSE,
  106.                                     WA_CloseGadget, TRUE,
  107.                                     WA_IDCMP,       IDCMP_CLOSEWINDOW,
  108.                                     WA_DragBar,     TRUE,
  109.                                     WA_DepthGadget, TRUE,
  110.                                     WA_Title,       (ULONG)"AvailFonts() example",
  111.                                     TAG_END))
  112.             {
  113.               myrp = *(mywin->RPort);       /* A structure assign: clone my window's Rastport. */
  114.                                             /* RastPort.  This RastPort will be used to render */
  115.                                             /* the font specs, not the actual font sample.     */
  116.               if (mydrawinfo = GetScreenDrawInfo(mywin->WScreen))
  117.               {
  118.                 SetFont(&myrp, mydrawinfo->dri_Font);
  119.  
  120.                 myrect.MinX = mywin->BorderLeft;                          /* LAYOUT THE WINDOW */
  121.                 myrect.MinY = mywin->BorderTop;
  122.                 myrect.MaxX = mywin->Width - (mywin->BorderRight + 1);
  123.                 myrect.MaxY = mywin->Height - (mywin->BorderBottom + 1);
  124.  
  125.                 cliprectside = (myrect.MaxX - myrect.MinX) / 20;
  126.  
  127.                 fname.charcount    = StrLen(fname.string);
  128.                 fheight.charcount  = StrLen(fheight.string);
  129.                 XDPI.charcount     = StrLen(XDPI.string);
  130.                 YDPI.charcount     = StrLen(YDPI.string);
  131.                 entrynum.charcount = StrLen(entrynum.string);
  132.                 alphabetcharcount  = StrLen(alphabetstring);
  133.  
  134.                 fontheight = (myrp.Font->tf_YSize) + 2;
  135.  
  136.                 if (fontheight > ((myrect.MaxY - myrect.MinY) / 6))  /* If the default screen  */
  137.                 {                                                    /* font is more than one- */
  138.                   defaultfont = OpenFont(&defaultfontattr);          /* sixth the size of the  */
  139.                   SetFont(&myrp, defaultfont);                       /* window, use topaz-9.   */
  140.                   fontheight = (myrp.Font->tf_YSize) + 2;
  141.                 }
  142.  
  143.                 fname.stringwidth   = TextLength(&myrp, (STRPTR)fname.string, fname.charcount);
  144.                 fheight.stringwidth = TextLength(&myrp, (STRPTR)fheight.string, fheight.charcount);
  145.                 XDPI.stringwidth    = TextLength(&myrp, (STRPTR)XDPI.string, XDPI.charcount);
  146.                 YDPI.stringwidth    = TextLength(&myrp, (STRPTR)YDPI.string, YDPI.charcount);
  147.                 entrynum.stringwidth =
  148.                     TextLength(&myrp, (STRPTR)entrynum.string, entrynum.charcount);
  149.  
  150.                 stringwidth = fname.stringwidth;         /* What is the largest string length? */
  151.                 stringwidth =
  152.                     (fheight.stringwidth > stringwidth) ? fheight.stringwidth : stringwidth;
  153.                 stringwidth = (XDPI.stringwidth > stringwidth) ? XDPI.stringwidth : stringwidth;
  154.                 stringwidth = (YDPI.stringwidth > stringwidth) ? YDPI.stringwidth : stringwidth;
  155.                 stringwidth =
  156.                     (entrynum.stringwidth > stringwidth) ? entrynum.stringwidth : stringwidth;
  157.                 stringwidth += mywin->BorderLeft;
  158.  
  159.                 if (stringwidth < ((myrect.MaxX - myrect.MinX) >> 1)) /* If the stringwidth is */
  160.                 {                                                 /* more than half the viewing*/
  161.                   SetAPen(&myrp, mydrawinfo->dri_Pens[TEXTPEN]);  /* area, quit because the    */
  162.                   SetDrMd(&myrp, JAM2);                           /* font is just too big.     */
  163.  
  164.                   Move(&myrp, myrect.MinX + 8 + stringwidth - fname.stringwidth,
  165.                                myrect.MinY + 4 + (myrp.Font->tf_Baseline));
  166.                   Text(&myrp, fname.string, fname.charcount);
  167.  
  168.                   Move(&myrp, myrect.MinX + 8 + stringwidth - fheight.stringwidth,
  169.                               myrp.cp_y + fontheight);
  170.                   Text(&myrp, fheight.string, fheight.charcount);
  171.  
  172.                   Move(&myrp, myrect.MinX + 8 + stringwidth - XDPI.stringwidth,
  173.                               myrp.cp_y + fontheight);
  174.                   Text(&myrp, XDPI.string, XDPI.charcount);
  175.  
  176.                   Move(&myrp, myrect.MinX + 8 + stringwidth - YDPI.stringwidth,
  177.                               myrp.cp_y + fontheight);
  178.                   Text(&myrp, YDPI.string, YDPI.charcount);
  179.  
  180.                   Move(&myrp, myrect.MinX + 8 + stringwidth - entrynum.stringwidth,
  181.                               myrp.cp_y + fontheight);
  182.                   Text(&myrp, entrynum.string, entrynum.charcount);
  183.  
  184.                   myrect.MinX = myrect.MinX + cliprectside;
  185.                   myrect.MaxX = myrect.MaxX - cliprectside;
  186.                   myrect.MinY = myrect.MinY + (5 * fontheight) + 8;
  187.                   myrect.MaxY = myrect.MaxY - 8;
  188.  
  189.                   SetAPen(&myrp, mydrawinfo->dri_Pens[SHINEPEN]);         /* Draw a box around */
  190.                   Move(&myrp, myrect.MinX - 1, myrect.MaxY + 1);          /* the cliprect.     */
  191.                   Draw(&myrp, myrect.MaxX + 1, myrect.MaxY + 1);
  192.                   Draw(&myrp, myrect.MaxX + 1, myrect.MinY - 1);
  193.  
  194.                   SetAPen(&myrp, mydrawinfo->dri_Pens[SHADOWPEN]);
  195.                   Draw(&myrp, myrect.MinX - 1, myrect.MinY - 1);
  196.                   Draw(&myrp, myrect.MinX - 1, myrect.MaxY);
  197.  
  198.                   SetAPen(&myrp, mydrawinfo->dri_Pens[TEXTPEN]);
  199.                                           /* Fill up a buffer with a list of the available fonts */
  200.                   afsize = AvailFonts((STRPTR)afh, 0L, AFF_MEMORY|AFF_DISK|AFF_SCALED|AFF_TAGGED);
  201.                   do
  202.                   {
  203.                     afh = (struct AvailFontsHeader *) AllocMem(afsize, 0);
  204.                     if (afh)
  205.                     {
  206.                       afshortage = AvailFonts((STRPTR)afh, afsize,
  207.                                               AFF_MEMORY|AFF_DISK|AFF_SCALED|AFF_TAGGED);
  208.                       if (afshortage)
  209.                       {
  210.                         FreeMem(afh, afsize);
  211.                         afsize += afshortage;
  212.                         afh = (struct AvailFontsHeader *)(-1L);
  213.                       }
  214.                     }
  215.                   } while (afshortage && afh);
  216.  
  217.                   if (afh)
  218.                   {
  219.                     /* This is for the layers.library clipping region that gets attached to */
  220.                     /* the window.  This prevents the application from unnecessarily        */
  221.                     /* rendering beyond the bounds of the inner part of the window. For     */
  222.                     /* more information on clipping, see the Layers chapter of this manual. */
  223.  
  224.                     if (new_region = NewRegion())                 /* More layers stuff */
  225.                     {
  226.                       if (OrRectRegion(new_region, &myrect)) /* Even more layers stuff */
  227.                       {
  228.                          /* Obtain a pointer to the window's rastport and set up some of    */
  229.                          /* the rastport attributes.  This example obtains the text pen     */
  230.                          /* for the window's screen using the GetScreenDrawInfo() function. */
  231.                          mycliprp = mywin->RPort;
  232.                          SetAPen(mycliprp, mydrawinfo->dri_Pens[TEXTPEN]);
  233.  
  234.                          MainLoop();
  235.                       }
  236.                       DisposeRegion(new_region);
  237.                     }
  238.                     FreeMem(afh, afsize);
  239.                   }
  240.                 }
  241.                 FreeScreenDrawInfo(mywin->WScreen, mydrawinfo);
  242.               }
  243.               CloseWindow(mywin);
  244.             }
  245.             CloseLibrary(UtilityBase);
  246.           }
  247.           CloseLibrary(LayersBase);
  248.         }
  249.         CloseLibrary(GfxBase);
  250.       }
  251.       CloseLibrary(IntuitionBase);
  252.     }
  253.     CloseLibrary(DiskfontBase);
  254.   }
  255. }
  256.  
  257.  
  258.  
  259. void MainLoop(void)
  260. {
  261.   UWORD x;
  262.   struct Task *mytask;
  263.   struct IntuiMessage *mymsg;
  264.   BOOL aok = TRUE;
  265.   struct TAvailFonts *afont;
  266.   struct TextFont *myfont;
  267.   UBYTE buf[8];
  268.   ULONG dpi;
  269.  
  270.   mytask = FindTask(NULL);
  271.   afont = (struct TAvailFonts *)&(afh[1]);
  272.  
  273.   for (x = 0; (x < afh->afh_NumEntries); x++)
  274.   {
  275.     if (aok)
  276.     {
  277.       if (myfont = OpenDiskFont(&(afont->taf_Attr)))
  278.       {
  279.         SetAPen(&myrp, mydrawinfo->dri_Pens[BACKGROUNDPEN]);   /* Print the TextFont attributes. */
  280.         RectFill( &myrp, stringwidth, mywin->BorderTop + 4,
  281.                   mywin->Width - (mywin->BorderRight + 1), myrect.MinY - 2 );
  282.  
  283.         SetAPen(&myrp, mydrawinfo->dri_Pens[TEXTPEN]);
  284.         Move( &myrp, stringwidth + mywin->BorderLeft,
  285.               mywin->BorderTop + 4 + (myrp.Font->tf_Baseline) );
  286.         Text( &myrp, (UBYTE *)myfont->tf_Message.mn_Node.ln_Name,
  287.               StrLen((UBYTE *)myfont->tf_Message.mn_Node.ln_Name) );
  288.  
  289.         Move(&myrp, stringwidth + mywin->BorderLeft, myrp.cp_y + fontheight); /* Print the      */
  290.         sprintf(buf, "%d\0", myfont->tf_YSize);                               /* font's Y Size. */
  291.         Text(&myrp, buf, StrLen(buf));
  292.  
  293.         Move(&myrp, stringwidth + mywin->BorderLeft, myrp.cp_y + fontheight); /* Print the X DPI */
  294.         dpi = GetTagData( TA_DeviceDPI, 0L,
  295.                           ((struct TextFontExtension *)(myfont->tf_Extension))->tfe_Tags);
  296.         if (dpi)
  297.         {
  298.             sprintf(buf, "%d\0", ((dpi & 0xFFFF0000)>>16));
  299.             Text(&myrp, buf, StrLen(buf));
  300.         }
  301.         else Text(&myrp, "nil", 3L);
  302.  
  303.         Move(&myrp, stringwidth + mywin->BorderLeft, myrp.cp_y + fontheight); /* Print the Y DPI */
  304.         if (dpi)
  305.         {
  306.             sprintf(buf, "%d\0", (dpi & 0x0000FFFF));
  307.             Text(&myrp, buf, StrLen(buf));
  308.         }
  309.         else Text(&myrp, "nil", 3L);
  310.  
  311.         Move(&myrp, stringwidth + mywin->BorderLeft, myrp.cp_y + fontheight);     /* Print the */
  312.         sprintf(buf, "%d\0", x);                                                   /* entrynum. */
  313.         Text(&myrp, buf, StrLen(buf));
  314.  
  315.         SetFont(mycliprp, myfont);
  316.         old_region = InstallClipRegion(mywin->WLayer, new_region); /* Install clipping rectangle */
  317.  
  318.         SetRast(mycliprp, mydrawinfo->dri_Pens[BACKGROUNDPEN]);
  319.         Move( mycliprp, myrect.MinX, myrect.MaxY - (myfont->tf_YSize - myfont->tf_Baseline) );
  320.         Text(mycliprp, alphabetstring, alphabetcharcount);
  321.  
  322.         Delay(100);
  323.  
  324.         new_region = InstallClipRegion(mywin->WLayer, old_region);  /* Remove clipping rectangle */
  325.  
  326.         while (mymsg = (struct IntuiMessage *)GetMsg(mywin->UserPort))
  327.         {
  328.             aok = FALSE;
  329.             x = afh->afh_NumEntries;
  330.             ReplyMsg((struct Message *)mymsg);
  331.         }
  332.  
  333.  
  334.  
  335.         if (mytask->tc_SigRecvd & SIGBREAKF_CTRL_C)        /* Did the user hit CTRL-C (the shell */
  336.         {                                                  /* window has to receive the CTRL-C)? */
  337.             aok = FALSE;
  338.             x = afh->afh_NumEntries;
  339.             VPrintf("Ctrl-C Break\n", NULL);
  340.         }
  341.         CloseFont(myfont);
  342.       }
  343.     }
  344.     afont++;
  345.   }
  346. }
  347.  
  348.  
  349. ULONG StrLen(UBYTE *string)
  350. {
  351.     ULONG x = 0L;
  352.  
  353.     while (string[x++]);
  354.     return(--x);
  355. }
  356.